﻿package framework.core;

import java.sql.*;
import java.text.*;
import javax.naming.*;
import javax.sql.DataSource;

/**
 * Creation date: (2003-08-28)
 * @author: 田文斌
 * 数据库链接包装类
 */

public class DBConnection 
{
	
    private int num;
    
    public static int count = 0;
    
	private java.sql.Connection m_con ;
	
	private static int m_instance_count = 0 ;
	private static int getInstanceCount()
	{
		m_instance_count ++ ;
		return m_instance_count;
	}
	private int m_id = 0 ;
	public int getId()
	{
		return m_id ;
	}
	
	private DBConnection(java.sql.Connection con)
	{
	    num = ++count;
		m_con = con ;
		m_id = getInstanceCount();
	}
	
	//通过jdbc访问数据库
	private static DBConnection open(String driverClass,String dbUrl) throws Exception 
	{
		Class.forName(driverClass);
		DBConnection con = new DBConnection(DriverManager.getConnection(dbUrl));
		Logger.log(con,"opened,driverClass="+driverClass+",dbUrl="+dbUrl);
		return con ;
	}
	
	//通过数据源访问数据库
	private static DBConnection open(String driverClass,String dataSource,String user,String password) throws Exception 
	{
		if(Config.getWebServerType().equalsIgnoreCase(Config.WEBSERVER_WEBLOGIC))
		{
			Class.forName(driverClass);
			InitialContext initialcontext = new InitialContext();
			DataSource ds = (DataSource)initialcontext.lookup("java:comp/env/" + Config.getDBDataSource());
			return new DBConnection(ds.getConnection());
		}
		Class.forName(driverClass);
		Context initCtx = new InitialContext();
		Context ctx = (Context) initCtx.lookup("java:comp/env");
		Object obj = (Object) ctx.lookup(dataSource); 
		DataSource ds = (javax.sql.DataSource)obj;
		return new DBConnection(ds.getConnection(user,password));
	}
	
	public static DBConnection open(boolean isDev) throws Exception
	{
		String driverClass = Config.getDBDriverClass();
		String dataSource = Config.getDBDataSource();
		String dbUrl = Config.getDBUrl();
		String user = Config.getDBUser();
		String password = Config.getDBPassword();
		String devDataSource = Config.getDevDBDataSource();
		String devDBUrl = Config.getDevDBUrl();
		String devUser = Config.getDevDBUser();
		String devPassword = Config.getDevDBPassword();

		if (isDev)
		{
			if (devDataSource != null)
			{
				return open(driverClass, devDataSource, devUser, devPassword);
			}
			return open(driverClass, "jdbc:oracle:thin:" + devUser + "/"
					+ devPassword + "@" + devDBUrl);
		} else
		{
		    
		    
		    
			if (dataSource != null)
			{
			    LogWriter.info("CONNECTION["+  count +"] OPENED.");
				return open(driverClass, dataSource, user, password);
			}

			
			LogWriter.info("CONNECTION["+  count +"] OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOPENED.");
			return open(driverClass, "jdbc:oracle:thin:" + user + "/"
					+ password + "@" + dbUrl);
		}
	}
	public static DBConnection open() throws Exception
	{
		return open(false);
	}
	
	/*
	public static DBConnection openOracle(String user,String password,String url) throws Exception 
	{
		String driverClass = "oracle.jdbc.driver.OracleDriver";
		String dbUrl = "jdbc:oracle:thin:"+user+"/"+password+"@"+url;
		return open(driverClass,dbUrl);
	}
	*/
	
	public void close() throws Exception
	{
		if(m_con!=null)
		{
			m_con.close();
			//Logger.log(this,"closed.");
			LogWriter.info("CONNECTION["+  this.num +"] CCCCCCCCCCCCCCCCCCCCCCCCCCCCLOSED");
		}
	}
	public void commit() throws Exception
	{
		if(m_con!=null)
		{
			m_con.commit();
			//Logger.log(this,"commit.");
		}
	}
	public void rollback() throws Exception
	{
		if(m_con!=null)
		{
			m_con.rollback();
			//Logger.log(this,"rollback.");
		}
	}

	public DBPreparedStatement prepareStatement(String sql)  throws Exception
	{
		if(m_con!=null)
		{
			java.sql.PreparedStatement pst = m_con.prepareStatement(sql);
			DBPreparedStatement dbPst = new DBPreparedStatement(pst,sql);
			return dbPst ;
		}
		return null;
	}
	
	public boolean isClosed() throws Exception
	{
		if(m_con!=null)
		{
			return m_con.isClosed();
		}
		return true;
	}
	
	public void setAutoCommit(boolean autoCommit) throws Exception
	{
		if(m_con!=null)
		{
			m_con.setAutoCommit(autoCommit);
		}
	}
	
	public String toString()
	{
		DecimalFormat fmt = new DecimalFormat("########");
		return "DBConnection-"+fmt.format(getId())+"" ;
	}
	
	public Connection getConnection()
	{
		return m_con;
	}
	
	//通过jdbc访问数据库
	public static DBConnection openByJdbc() throws Exception 
	{
		Class.forName(Config.getDBDriverClass());
		DBConnection con = open(Config.getDBDriverClass(), "jdbc:oracle:thin:" + Config.getDBUser() + "/" + Config.getDBPassword() + "@" + Config.getDBUrl());
		return con ;
	}
	
	private String area = "";
	public String getArea()
	{
		return area;
	}

	public void setArea(String area)
	{
		this.area = area;
	}
	
	private boolean isNotReplace = false;
	public boolean isNotReplace()
	{
		return isNotReplace;
	}

	public void setNotReplace(boolean isNotReplace)
	{
		this.isNotReplace = isNotReplace;
	}
	
	private boolean isHQ = false;
	public boolean isHQ()
	{
		return isHQ;
	}

	public void setHQ(boolean isHQ)
	{
		this.isHQ = isHQ;
	}
	
	
	
}
